// .........................................................................
// Title: Master_Build.do
//
// Overall context manager for Stata build jobs
// .........................................................................

* ---------------------------------------
* Set up environment
* ---------------------------------------

* main globals
global whoami <WHOAMI>
global code_path <CODE_PATH>
global project_path <PROJECT_PATH>
global python_interpreter_path <PYTHON_INTERPRETER_PATH>

* paths
global raw "$project_path/raw"
global logs "$project_path/logs"
global tmp "$project_path/temp"
global temp "$project_path/temp"
global graphs "$project_path/graphs"
global output "$project_path/output"
global tables "$project_path/tables"
global holdings "$project_path/holdings"
global morningstar_hd "$output/morningstar_holding_detail"
global insurance_holdings "$project_path/insurance_master"

* create folders
cap mkdir $logs
cap mkdir $tmp
cap mkdir $temp
cap mkdir $graphs
cap mkdir $output
cap mkdir $tables
cap mkdir $holdings
cap mkdir $morningstar_hd
cap mkdir $insurance_holdings

* log file
cap mkdir "$logs/`2'"
cap log close 
log using "$logs/`2'/${whoami}_`2'.log", replace

* load parameters and programs
do $code_path/scripts/build_params.do
do $code_path/scripts/gen_duration_category.do

* scheme, excel settings
set scheme shplot
set excelxlsxlargefile on

* ---------------------------------------
* Launch job
* ---------------------------------------

if "`2'" == "Moodys_DRD" {
    do $code_path/build/moodys_drd.do
}

if "`2'" == "Factset_DCS" {
    do $code_path/build/factset_dcs.do
}

if "`2'" == "Mergent" {
    do $code_path/build/mergent.do
}

if "`2'" == "CIQ_Characteristics" {
    do $code_path/build/ciq_characteristics.do
}

if "`2'" == "Macro_Series" {
    do $code_path/build/macro_series.do
}

if "`2'" == "Dealogic_DCM" {
    do $code_path/build/dealogic_dcm.do
}

if "`2'" == "Sovereign_Data" {
    do $code_path/build/sovereign_data.do
}

if "`2'" == "WRDS_Characteristics" {
    do $code_path/build/wrds_characteristics.do
}

if "`2'" == "Compustat" {
    do $code_path/build/compustat.do
}

if "`2'" == "Morningstar_Industry" {
    do $code_path/build/industry_morningstar.do
}

if "`2'" == "Fund_Holdings_Summary" {
    do $code_path/build/fund_holdings_summary.do
}

if "`2'" == "Interest_Details" {
    do $code_path/build/interest_details.do
}

if "`2'" == "Process_TRACE" {
    do $code_path/build/process_trace.do
}

if "`2'" == "Consolidate_Duration" {
    do $code_path/build/consolidate_duration.do
}

if "`2'" == "Excess_Returns" {
    do $code_path/build/excess_returns.do
}

if "`2'" == "Amounts_Outstanding" {
    do $code_path/build/amounts_outstanding.do
}

if "`2'" == "Consolidate_Holdings" {
    do $code_path/build/consolidate_holdings.do
}

if "`2'" == "Consolidate_Ratings" {
    do $code_path/build/consolidate_ratings.do
}

if "`2'" == "Price_Characteristics_Panel" {
    do $code_path/build/price_characteristics_panels.do
}

if "`2'" == "Bond_Betas" {
    do $code_path/build/bond_betas.do
}

if "`2'" == "Industry_Master" {
    do $code_path/build/industry_master.do
}

if "`2'" == "Insurance_Panel" {
    do $code_path/build/insurance_panel.do
}

if "`2'" == "Bartik_Instrument" {
    do $code_path/build/bartik_instrument.do
}

if "`2'" == "Drawdowns_Sample" {
    do $code_path/build/drawdowns_sample.do
}

if "`2'" == "Firm_Outcomes" {
    do $code_path/build/firm_outcomes.do
}

if "`2'" == "NonUS_Panel" {
    do $code_path/build/nonus_panel.do
}

cap log close
